package com.ele.client.editor;

import com.github.gwtbootstrap.client.ui.NavPills;
import com.github.gwtbootstrap.client.ui.Scrollspy;
import com.github.gwtbootstrap.client.ui.base.DivWidget;
import com.github.gwtbootstrap.client.ui.base.HasId;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;

public class Subnav extends DivWidget implements HasId {

    private NavPills container = new NavPills();
    private boolean scrollspy;
    private Scrollspy spy;
    private boolean isFixed;
    int navtop;

    public Subnav() {
        super("subnav subnav-fixed");
        super.add(container);
        setId(DOM.createUniqueId());

//        Window.addWindowScrollHandler(new Window.ScrollHandler() {
//
//            @Override
//            public void onWindowScroll(Window.ScrollEvent event) {
//                int scrollTop = Window.getScrollTop();
//                GWT.log(navtop + "");
//                GWT.log(scrollTop + "");
//                if (scrollTop >= navtop && !isFixed) {
//                    GWT.log("apply");
//                    isFixed = true;
//                    Subnav.this.addStyleName("subnav-fixed");
//                } else if (scrollTop <= navtop && isFixed) {
//                    isFixed = false;
//                    Subnav.this.removeStyleName("subnav-fixed");
//                }
//            }
//        });
    }

    /**
     * {@inheritDoc}
     */
    public void setId(String id) {
        getElement().setId(id);
    }

    /**
     * {@inheritDoc}
     */
    public String getId() {
        return getElement().getId();
    }

    /**
     * Defines whether the Navbar should contain a {@link Scrollspy}.
     *
     * @param scrollspy <code>true</code> to include a Scrollspy. Default:
     *                  <code>false</code>
     */
    public void setScrollspy(boolean scrollspy) {
        this.scrollspy = scrollspy;
        if (scrollspy) {
            spy = new Scrollspy();
            spy.setTarget(getId());
        }
    }

    /**
     * Defines scrollspy target element.
     *
     * @param spyElement target element
     */
    public void setSpyElement(Element spyElement) {

        assert spyElement != null;

        if (spy == null) {
            spy = new Scrollspy();
            spy.setTarget(getId());
        }

        spy.setSpyElement(spyElement);
        this.scrollspy = true;
    }

    @Override
    protected void onAttach() {
        super.onAttach();
        if (spy == null) {
            spy = new Scrollspy();
            spy.setTarget(getId());
        }

        if (scrollspy) {
            spy.configure();
        }

        navtop = this.getOffsetTop(getElement()) - 88;
        // TODO make a unconfigure feature.
    }

    private native int getOffsetTop(Element e) /*-{
        return $wnd.jQuery(e).offset().top;
    }-*/;

    @Override
    public void add(Widget w) {
        container.add(w);
    }

    public Scrollspy getSpy() {
        return spy;
    }

    @Override
    public void clear() {
        container.clear();
    }
}